import {Component, OnDestroy, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute, Router} from "@angular/router";
import {NzMessageService} from "ng-zorro-antd/message";
import {NzModalService} from "ng-zorro-antd/modal";
import {Customer2Service} from "../../../../shared/services/product2/customer.service";

@Component({
    selector: 'app-customer-list',
    templateUrl: './customer-list.component.html',
    styleUrls: ['./customer-list.component.scss']
})
export class CustomerListComponent implements OnInit, OnDestroy {
    option: any = {
        keyword: '',
        level: 'all',
        fields: ['name', 'person']
    };

    customers: Array<any> = [];
    _customers: Array<any> = [];

    _subscriptions: Array<any> = [];

    constructor(private router: Router, public route: ActivatedRoute, private customerService: Customer2Service, 
        private modal: NzModalService, private message: NzMessageService) {
    }

    ngOnInit() {
        this.customerService.getAll()
            .subscribe(res => {
                this._customers = res;
                this.clear();
            });
        this._subscriptions.push(this.customerService.on( 'create',res => {
            this._customers.push(res);
            this.filter();
        }));
        this._subscriptions.push(this.customerService.on( 'delete',res => {
            const index = this._customers.findIndex(item => item._id === res._id);
            index !== -1 && this._customers.splice(index, 1);
            this.filter();
        }));
        this._subscriptions.push(this.customerService.on( 'update',res => {
            const index = this._customers.findIndex(item => item._id === res._id);
            index !== -1 && this._customers.splice(index, 1, res);
            this.filter();
        }));
    }

    ngOnDestroy(): void {
        this._subscriptions.forEach( sub => {
            sub.unsubscribe();
        });
    }

    clear() {
        this.option.keyword = '';
        this.filter();
    }

    filter() {
        this.customers = this._customers.filter(item => {
            if (this.option.level != 'all' && item.level != this.option.level) {
                return false;
            }
            if (this.option.keyword.trim() === '') {
                return true;
            } else {
                return this.option.fields.some(field => {
                    if (item[field].indexOf(this.option.keyword) != -1) {
                        return true;
                    }
                    return false;
                });
            }
        });
    }

    activeLevel(level) {
        this.option.level = level;
        this.filter();
    }

    edit(item) {
        this.router.navigate(['detail',item._id], {relativeTo: this.route.parent});
    }

    delete(item) {
        this.modal.confirm({
            nzTitle: '提示！',
            nzContent: `是否确认删除？`,
            nzOkText: '确定',
            nzOkType: 'primary',
            nzOnOk: () => {
                this.customerService.delete(item._id)
                    .subscribe(res => {
                        if (res.result){
                            this.message.create('success', '删除成功！');
                            this.customerService.emit('delete', item);
                        } else {
                            this.message.create('warning', '删除失败！');
                        }
                    });
            }
        });
    }

    submit(event) {
        if (!event.result) return;
        if (event.mode === 0) {
            this.customerService.emit('create', event.object);
        } else {
            this.customerService.emit('update', event.object);
        }
    }

}
